
function click_marker(pic,latlng,map) {
return function(e) {
  e.cancelBubble = true;
  e.returnValue = false;
  if (e.stopPropagation) {
    e.stopPropagation();
    e.preventDefault();
  }
  var title = pic.photo_title;
  var content = pic.photo_content;
  var image = pic.photo_file_url;
  var url = pic.url;
  var url_360 = pic.url_360;
  var bed = pic.bed;
  var home = pic.home;
  var show = pic.show;

  var infoHtml = '' +
  '<div style="width:390px;height:120px;">' +
	'<div style="max-height:120px;overflow:hidden;margin:0;padding:0;float:left;"><img src="'+image+'" style="width:180px;" /></div>' +
	'<div style="float:right;width:170px;">' +
		'<h4 style="color:rgb(15, 36, 74);margin:0;padding:0;text-overflow: ellipsis;white-space: nowrap;overflow: hidden;" title="'+title+'">'+title+'</h4>' +
		'<p style="margin:3px 0;">'+content+'</p>' +
		'<p><a href="'+url+'" style="text-decoration:underline;margin-right:10px;">photo gallery</a> <a href="'+url_360+'" style="text-decoration:underline;">virtual tour</a></p>' +
		'<div class="map-ico"><p class="ico"><img src="img/ico-bed.png"><span>3</span></p> ' +
		'<p class="ico"><img src="img/ico-house.png"><span>2</span></p> ' +
		'<p class="ico"><img src="img/ico-show.png"><span>3</span></p></div>' +
		'</div>' +
  '</div>';

  infoWindow.setContent(infoHtml);
  infoWindow.setPosition(latlng);
  infoWindow.open(map);
};
}
function getOuter(i,dataPhoto,map,markers,length){
	var converter = new google.maps.Geocoder();
	converter.geocode( { 'address': dataPhoto.address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
		  latitude_row = results[0].geometry.location.lat();
		  longitude_row = results[0].geometry.location.lng();
	      var latLng = new google.maps.LatLng(latitude_row,
	          longitude_row);
	      var marker = new google.maps.Marker({
	        position: latLng,
	        title: dataPhoto.photo_title,
	      });
	      var fn = click_marker(dataPhoto,latLng,map);
	      google.maps.event.addListener(marker, 'click', fn);
	      markers.push(marker);
	      if(i==length-1) markcluster = new MarkerClusterer(map, markers);
	    } 
	}); 
}
function initialize() {
	// Chuyển ADDRESS sang TOẠ ĐỘ
	var latitude, longitude, markcluster;
	var geocoder = new google.maps.Geocoder();
	var address = "Australia";
	geocoder.geocode( { 'address': address}, function(results, status) {
	if (status == google.maps.GeocoderStatus.OK) {
	    latitude = results[0].geometry.location.lat();
	    longitude = results[0].geometry.location.lng();
	    // Khởi tạo MAP
	    var center = new google.maps.LatLng(latitude, longitude);
	  
	    var map = new google.maps.Map(document.getElementById('googlemaps'), {
	      zoom: 4,
	      center: center,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    });
	    // Tạo MARKER
	    var markers = [];
	    var photos = [];

	    // Virtual Data
	    photos[0] = {"photo_title": "Oakbridge", "photo_file_url": "img/360-1.png", "address": "502 Bourke St Melbourne Victoria 3000 Australia", "url": "home-details.html","url_360": "home-details-360.html", "photo_content": "17 17.5sqm", "home": 1, "bed": 2, "show": 3};
	    photos[1] = {"photo_title": "Oakbridge", "photo_file_url": "img/360-2.png", "address": "510 Bourke St Melbourne Victoria 3000 Australia", "url": "home-details.html","url_360": "home-details-360.html", "photo_content": "17 17.5sqm", "home": 1, "bed": 2, "show": 3};

		 for (var i = 0; i < photos.length; i++) {
		 	var dataPhoto = photos[i];
	        getOuter(i,dataPhoto,map,markers,photos.length);
		}
	  } 
	}); 
}
var infoWindow = new google.maps.InfoWindow();
google.maps.event.addDomListener(window, 'load', initialize);